Ext.namespace("Dudley");
Ext.namespace("Dudley.lims");

Dudley.lims.gallerymgr = function() {
    var loaded = false;
    var galleryPanel = {};
    Ext.History.init();
    var tokenDelimiter = ':';

    return {
        getGalleryLoaded: function() {
            return loaded;
        },
        getGalleryPanel: function() {
            return galleryPanel;
        },
        clearPane: function() {
            Ext.getDom("gallery-form").innerHTML = "";
        },
        loadNewickText: function(){
                var layoutval = Ext.getCmp("phylolayoutId").getValue().inputValue;
                var text = Ext.getCmp("newickinputId").getValue();
                Ext.getCmp("newickwinId").close();
                Dudley.lims.PhyloTree.loadPhyloFromPrompt(text, layoutval);
        },
        loadGallery: function() {
            var pvwin = new Ext.Window({
                title: 'Phylo Gallery Loading...',
                width: 400,
                minWidth: 350,
                height: 150,
                modal: true,
                closeAction: 'hide',
                bodyStyle: 'padding:10px;',
                html: "Initializing Gallery...",
                bbar: new Ext.ux.StatusBar({
                    id: 'init-yo-statusbar',
                    defaultText: 'Ready'
                })
            });
            pvwin.on("show", function () {
                var sb = Ext.getCmp("init-yo-statusbar");
                sb.showBusy();
            });
            pvwin.show();
            var mytbar = [
            {
                id: 'phyloPromptId',
                disabled: false,
                text: 'View New Phylotree',
                icon: "images/check-green.png",
                cls:'x-btn-text-icon',
                tooltip: 'Displays phylogenetic tree',
                listeners: {click:
                        function() {
                            var simple = new Ext.FormPanel({
                            frame:true,
                            title: 'New Phylogenetic Tree',
                            bodyStyle:'padding:5px 5px 0',
                            width: 450,
                            items: [
                                new Ext.form.RadioGroup({
                                checked: true,
                                columns: 2,
                                fieldLabel: 'Tree Layout',
                                id: 'phylolayoutId',
                                items: [
                                    {boxLabel: 'Circular', name: 'phylolayout', inputValue: 'circular', checked: true},
                                    {boxLabel: 'Rectangular', name: 'phylolayout', inputValue: 'rectangular'}]
                                }),
                            new Ext.form.TextArea({
                            id: "newickinputId",
                            fieldLabel: "Enter in newick text(replace example, no quotes):",
                            value: '(((Espresso:2,(Milk Foam:2,Espresso Macchiato:5,((Steamed Milk:2,Cappuccino:2,(Whipped Cream:1,Chocolate Syrup:1,Cafe Mocha:3):5):5,Flat White:2):5):5):1,Coffee arabica:0.1,(Columbian:1.5,((Medium Roast:1,Viennese Roast:3,American Roast:5,Instant Coffee:9):2,Heavy Roast:0.1,French Roast:0.2,European Roast:1):5,Brazilian:0.1):1):1,Americano:10,Water:1);',
                            width: 380,
                            height: 200,
                            allowBlank: false
                            })
                        ]
                    });
                   var  myWin = new Ext.Window({
                layout:'fit',
                id: 'newickwinId',
                width:500,
                height:500,
                closeAction:'hide',
                plain: true,
                items: simple,
                buttons: [{
                    text:'Submit',
                    handler: function() {
                        Dudley.lims.gallerymgr.loadNewickText();
                    }
                }]
            });
                 myWin.show();
                        }
                    }
            }];
            if (!loaded) {
                    loadKaryomapStatic("pg-canvas", function(hapform) {
                    pvwin.close();
                    loaded = true;
                    galleryPanel = new Ext.TabPanel({
                        autoHeight: true,
                        tbar: mytbar,
                        activeTab: 1,
                        border: true,
                        renderTo: "content-gallery",
                        items: [ hapform, Dudley.lims.PhyloTree.getPhyloPanel()]
                    });

                });
            }
        }
    };
}();
